package cn.yunhe.service.serviceImpl;

import cn.yunhe.dao.RoleDao;
import cn.yunhe.entity.Role;
import cn.yunhe.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> selectRoleByUserId(Integer userId) {
        return roleDao.selectRoleByUserId(userId);
    }

    @Override
    public void addRoleToUser(Integer[] ids, Integer userId) {
        for (Integer roleId : ids) {
            roleDao.insertUr(userId, roleId);
        }
    }

    @Override
    public List<Role> findById(Integer userId) {
        return roleDao.findById(userId);
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public void insertRole(Role role) {
        roleDao.insertRole(role);
    }

    @Override
    public Role findRoleByIdAndAllPermission(Integer id) {
        return roleDao.findRoleByIdAndAllPermission(id);
    }

    @Override
    public void addPermissionToRole(Integer[] ids, Integer roleId) {
        for (Integer PId : ids) {
            roleDao.addRp(PId, roleId);
        }
    }

    @Override
    public Role findRoleById(Integer id) {
        return roleDao.findRoleById(id);
    }

    @Override
    public void deleteRole(Integer id) {
        roleDao.deleteRp(id);
        roleDao.deleteRole(id);
    }

}
